package com.ctrip.ec.api.admin.model.query;

import com.ctrip.ec.dal.PagerSetting;
import com.google.common.base.Strings;

import java.util.List;


/**
 * Created by jiangzy on 2018/8/23.
 */
public class QueryModel {

    private List<String> sort;
    private List<FilterItem> filter;
    private Integer pageIndex;
    private Integer pageSize;

    public Integer getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public List<String> getSort() {
        return sort;
    }

    public void setSort(List<String> sort) {
        this.sort = sort;
    }

    public List<FilterItem> getFilter() {
        return filter;
    }

    public void setFilter(List<FilterItem> filter) {
        this.filter = filter;
    }

    public PagerSetting build() {
        PagerSetting pagerSetting = new PagerSetting();
        if (pageIndex != null && pageIndex.intValue() != 0) {
            pagerSetting.setPageIndex(pageIndex);
        }
        if (pageSize != null && pageSize.intValue() != 0) {
            pagerSetting.setPageSize(pageSize);
        }

        if (sort != null) {
            String orderBy = "";

            for (String s : sort) {
                orderBy += ",";
                String first = s.substring(0, 1);


                if (first.equals("-")) {
                    orderBy += convertToDbfield(s.substring(1)) + " desc";
                } else if (first.equals("+")) {
                    orderBy += convertToDbfield(s.substring(1));
                } else {
                    orderBy += convertToDbfield(s);
                }

            }
            if (orderBy.length() > 1) {
                pagerSetting.setOrderBy(orderBy.substring(1));
            }
        }

        if (filter != null) {
            StringBuilder sb = new StringBuilder();
            for (FilterItem f : filter) {
                String dbFieldName = convertToDbfield(f.getFieldName());
                sb.append(" and ");

                if (f.getOperator().equals("equals")) {
                    if (f.getFieldType().equals("number")) {
                        sb.append(dbFieldName + "=" + f.getValue());
                    } else {
                        sb.append(dbFieldName + "='" + f.getValue() + "'");
                    }
                } else if (f.getOperator().equals("contains")) {
                    sb.append(dbFieldName + " like '%" + f.getValue() + "%'");
                } else if (f.getOperator().toLowerCase().equals("inrange")) {
                    if (f.getFieldType().equals("number")) {
                        if (!Strings.isNullOrEmpty(f.getFrom())) {
                            sb.append(dbFieldName + " >=" + f.getFrom());
                            if (!Strings.isNullOrEmpty(f.getTo())) {
                                sb.append(" and " + dbFieldName + " <=" + f.getTo());
                            }
                        } else if (!Strings.isNullOrEmpty(f.getTo())) {
                            sb.append(dbFieldName + " <=" + f.getTo());
                        }

                    } else {
                        if (!Strings.isNullOrEmpty(f.getFrom())) {
                            sb.append(dbFieldName + " >='" + f.getFrom() + "'");
                            if (!Strings.isNullOrEmpty(f.getTo())) {
                                sb.append(" and " + dbFieldName + " <='" + f.getTo() + "'");
                            }
                        } else if (!Strings.isNullOrEmpty(f.getTo())) {

                            sb.append(dbFieldName + " <='" + f.getTo() + "'");

                        }
                    }
                }
            }
            if (sb.length() > 0) {
                pagerSetting.setWhere(sb.toString().substring(4));

            }
        }
        return pagerSetting;
    }

    private String convertToDbfield(String fieldName) {
        StringBuilder target = new StringBuilder(fieldName.length());

        for (Character ch : fieldName.toCharArray()) {
            if (ch.toString().equals(ch.toString().toUpperCase())) {
                target.append("_").append(ch.toString().toLowerCase());
            } else {
                target.append(ch.toString());
            }
        }
        return target.toString();
    }
}
